package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectAll();
        return new PageInfo<>(permissions);
    }

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void reload() {
        //用集合存储存数据库中的权限表达式
        List<String> permissions = permissionMapper.listAllExpressions();

        //从Spring容器中获取带有@Controller注解的bean（获取每个控制器对象）
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        //获取所有控制器对象
        Collection<Object> controllers = map.values();
        //遍历控制器类
        for (Object controller : controllers) {
            //获取每个控制器类的方法
            Class<?> controllerClass = controller.getClass();
            Method[] declaredMethods = controllerClass.getDeclaredMethods();

            for (Method declaredMethod : declaredMethods) {
                //获取每个方法上的注解
                RequiredPermission annotation = declaredMethod.getAnnotation(RequiredPermission.class);
                //判断是否有贴自定义的权限注解
                if (annotation == null) {//没有就跳过当前方法，去到下一个方法
                    continue;
                }
                //从注解中获取权限表达式
                String name = annotation.name();
                String expression = annotation.expression();
                //判断数据库中是否存在权限表达式
                if (permissions.contains(expression)) {//若存在，则不加入
                    continue;
                }

                //封装权限对象
                Permission permission = new Permission();
                permission.setName(name);
                permission.setExpression(expression);
                //执行数据库插入语句
                permissionMapper.insert(permission);
                permissions.add(expression);
            }
        }
    }

    @Override
    public List<Permission> selectAll() {
        List<Permission> permissions = permissionMapper.selectAll();
        return permissions;
    }

   //权限表达式
    public List<String> selectByEmpId(Long id) {
        List<String> expressions =  permissionMapper.selectByEmpId(id);
        return expressions;
    }
}
